/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Diagnoses;
import healthcare.coursework.model.DiagnosesPK;
import healthcare.coursework.model.Patient;
import healthcare.coursework.service.AppointmentService;
import healthcare.coursework.service.PatientService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class ViewAppointments extends GenericAutowireComposer {

    private Listbox appointmentDataListView;
    private Button appointmentCancel;
    private Label message;
    private Panel mainAppointment;
    private Patient p;
    private AppointmentService appointmentService = util.Utility.getUtility().getAppointmentService();
    private PatientService patientService = util.Utility.getUtility().getPatientService();




    public void onLoadForm(Event e) {
        setInfo();
    }

    private void setInfo() {
        Long patientId = Long.valueOf(Executions.getCurrent().getParameter("patientId"));
        p = patientService.findPatient(patientId);
        List<Diagnoses> appointments = appointmentService.listOfDiagnoseAppointments(patientId);
        for (int i = 0; i < appointments.size(); i++) {
            Diagnoses d = appointments.get(i);
            System.out.println(d.getDiagnosesPK());
        }
        appointmentDataListView.setModel(new ListModelList(appointments));
        if (appointments.isEmpty()) {
            mainAppointment.setVisible(false);
            message.setValue("There is no appointment associate with that patient");
            appointmentCancel.setVisible(false);
        }
    }

    public void onCancelAppointment(Event e) {
        try {
            Messagebox.show("Are you sure to cancel lab testings?", "Question", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    try {
                        if (((Integer) event.getData()).intValue() == Messagebox.YES) {
                            List<Listitem> items = new ArrayList(appointmentDataListView.getSelectedItems());
                            for (Listitem li : items) {
                                appointmentService.removeForever((DiagnosesPK) li.getValue());
                            }
                           execution.sendRedirect("/adminStaff/ViewAppointments.do?patientId="+p.getPatientId());
                        }
                    } catch (Exception ex) {
                        Messagebox.show("Please choose one the appointment to cancel?", "Warning", Messagebox.OK, Messagebox.INFORMATION);
                        ex.printStackTrace();
                    }
                }
            });

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public void onBookAppointment(Event e) {
        try {
            Map params = new HashMap();
            params.put("patient", p);
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/adminStaff/BookAppointment.zul", null, params);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
